﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class GetBufenUserHandler : IRequestHandler<GetBufenUserCommand, ApiResult<List<UserDTO>>>
    {
        private readonly IBaseRepository<UserModel> userRepo;
        private readonly IMapper mapper;

        public GetBufenUserHandler(IBaseRepository<UserModel> userRepo,IMapper mapper)
        {
            this.userRepo = userRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<UserDTO>>> Handle(GetBufenUserCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<UserDTO>> res = new ApiResult<List<UserDTO>>();
            var user = userRepo.GetAll().Where(x => x.Bid == request.Bid).ToList();
            var dto=mapper.Map<List<UserDTO>>(user);
            res.Msg = "查询用户信息成功";
            res.Data = dto;
            res.Code = ApiEnum.Success;
            return Task.FromResult(res);
        }
    }
}
